﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;

using ObjectGraphToTheEfnhX.UseService;


namespace ObjectGraphToTheEfnhX
{



    public partial class Form1 : Form
    {

        ForumerClient _svc = new ForumerClient();
        public Form1()
        {
            InitializeComponent();

            
            
        }


        private void uxNew_Click(object sender, EventArgs e)
        {
            bdsQuestion.DataSource = new Question();

            IndicateAnswerInputsAvailability();
        }

        private void uxSave_Click(object sender, EventArgs e)
        {
            bdsQuestion.EndEdit();
            bdsAnswerComment.EndEdit();
            bdsAnswer.EndEdit();

            ThreadPool.QueueUserWorkItem(o =>
            {
                Question question = (Question)bdsQuestion.Current;
                int id;
                byte[] rowVersion;
                id = _svc.SaveQuestion(out rowVersion, question);

                this.Invoke((MethodInvoker)delegate
                {
                    Question q = (Question)bdsQuestion.Current;
                    q.QuestionId = id;
                    q.RowVersion = rowVersion;
                });

                MessageBox.Show("Saved.");
            });
        }



        private void uxOpen_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(o =>
            {

                int n;
                bool isOk = int.TryParse(uxQuestionNavigator.Text, out n);
                if (isOk)
                {
                    Question q = _svc.OpenQuestion(n);

                    this.Invoke((MethodInvoker)delegate
                    {
                        bdsQuestion.DataSource = q;
                        IndicateAnswerInputsAvailability();
                    });
                }
            });




        }


        private void bdsQuestion_CurrentChanged(object sender, EventArgs e)
        {
            Console.WriteLine("bdsQuestion");

            var c = (Question)bdsQuestion.Current;

            if (c.Answers == null) c.Answers = new List<Answer>();
            if (c.Comments == null) c.Comments = new List<QuestionComment>();


        }

        void IndicateAnswerInputsAvailability()
        {
            bool readOnly = bdsAnswer.Current == null;


            uxAnswerText.ReadOnly = readOnly;
            uxAnswerPoster.ReadOnly = readOnly;

            grdAnswerComment.ReadOnly = readOnly;
            grdAnswerComment.DefaultCellStyle.BackColor = readOnly ? Color.LightGray : Color.White;
        }

        private void bdsAnswer_CurrentChanged(object sender, EventArgs e)
        {
            Console.WriteLine("bdsAnswer_CurrentChanged");




            var a = (Answer)bdsAnswer.Current;
            if (a == null)
            {
                IndicateAnswerInputsAvailability();
                return;
            }

            IndicateAnswerInputsAvailability();

            if (a.Question == null)
            {
                a.Question = (Question)bdsQuestion.Current; // link to parent

                // http://www.ienablemuch.com/2011/08/dont-initialize-collection-on-your.html
                a.Comments = new List<AnswerComment>();
            }
        }


        private void bdsQuestionComment_CurrentChanged(object sender, EventArgs e)
        {
            var c = (QuestionComment)bdsQuestionComment.Current;
            if (c == null) return;

            if (c.Question == null)
            {
                c.Question = (Question)bdsQuestion.Current; // link to parent                
            }

        }

        private void bdsAnswerComment_CurrentChanged(object sender, EventArgs e)
        {
            var ac = (AnswerComment)bdsAnswerComment.Current;
            if (ac == null) return;

            if (ac.Answer == null)
            {
                ac.Answer = (Answer)bdsAnswer.Current; // link to parent
            }



        }

        private void uxDelete_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                Question question = (Question)bdsQuestion.Current;

                _svc.DeleteQuestion(question.QuestionId, question.RowVersion);

                this.Invoke((MethodInvoker)delegate
                {
                    uxNew.PerformClick();
                });

                MessageBox.Show("Deleted.");
            });
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            _svc = new UseService.ForumerClient();

            uxNew.PerformClick();
        }





    }

    public static class Console
    {
        static int i = 0;
        public static void WriteLine(string s)
        {
            global::System.Console.WriteLine(s + " " + ++i);
        }
    }
}

